package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiyun.common.result.Result;
import com.jiyun.mapper.AddressMapper;
import com.jiyun.model.pojo.Address;
import com.jiyun.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("admin/address")
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private AddressMapper addressMapper;


    @PostMapping
    @ApiOperation("增加服务地址")
    public Result save(@RequestBody Address address){
        address.setIsDefault(0);
        addressService.save(address);
        return Result.ok();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据id删除服务地址")
    public Result remove(@PathVariable("id") Integer id){
        addressService.removeById(id);
        return Result.ok();
    }

    @PutMapping("/update")
    @ApiOperation("修改服务地址")
    public Result update(@RequestBody Address address){
        addressService.updateById(address);
        return Result.ok();
    }

    /**
     * 根据id查询所有地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getList(@PathVariable Integer id){
        List<Address> showlist = addressService.showlist(id);

        return Result.ok(showlist);
    }

    @GetMapping("/defeaul/{id}")
    @ApiOperation("查询默认地址")
    public Result getDefault(@PathVariable Integer id){
        Address address = addressService.getDefault(id);
        return Result.ok(address);
    }

    @GetMapping("/updatedefault/{id}/{addressId}")
    @ApiOperation("设置默认地址")
    public Result updateDefault(@PathVariable Integer id,@PathVariable Long addressId){
        addressService.updateDefault(id,addressId);
        return Result.ok();
    }
    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("根据id删除")
    public void deleteById(@PathVariable Integer id){
        addressService.deleteByUserId(id);
    }






}
